import * as api from "@/api"
export default{
    namespaced: true,
    state:{
        name:'product模块',
        products:[]
    },
    // 更改状态
    mutations:{
    // 设置产品数据
    SFT_PRODUCT(state,payloay){
        // console.log(payloay)
        state.products=(payloay)
    },
    DECREASE_INVENTORY(state,payload){
       //根据传入的数据,查找products中是否存在,如果存在,数量-1
       let target=state.products.find(item=>item.id==payload.id)
       target.inventory--
    },
    INCREASE_INVENTORY(state,payload){
        let target=state.products.find(item=>item.id==payload.id)
        target.inventory++
    }   
    },
    actions:{
        FETCH_PRODUCT({ commit, state }) {
            console.log('333', state.products)
            // if (state.products.length === 0) {
                // fetch("/api/data/index.json?data=v1")
                //     .then(body => body.json())
                //     .then(res => {
                //         // console.log(res)
                //         commit("SET_PRODUCT", res.data)
                //     })

                api.getProduct()
                .then(res=>{
                    console.log(res)
                    commit("SFT_PRODUCT", res.data.data)
                })
                // console.log(api)
            // }
        }
    },
    gettters:{},
}